<?php defined('SYSPATH') or die('No direct script access.');
 
class Controller_Registr extends Controller_Common  
{
   private function uploads_dir()
    {
        return DOCROOT . 'uploads/documents' . DIRECTORY_SEPARATOR;
    }
    public function addUser($role)
    {  
        
        if(isset($_POST['add']))
        {
            $valid = Captcha::valid($_POST['captcha'])? 1 : 2;
            
            if($valid==1)
            {
                $users = ORM::factory('user')
                    ->where('login', '=', $_POST['login'])
                    ->find();
                $us = ORM::factory('user')
                    ->where('email', '=', $_POST['email'])
                    ->find();
                if($users->loaded())
                {
                    echo "<script>alert('Такий логін вже існує, спробуйде ще раз!')</script>";
                }
                else
                {
                    if($us->loaded())
                    {
                        echo "<script>alert('Такий email вже існує, спробуйте ще раз!')</script>";
                    }
                    else
                    {
                        $users = ORM::factory('user');       
                        $users->initials = $_POST['initials'];
                        $users->login = $_POST['login'];
                        $users->password = $_POST['password'];
                        $users->email = $_POST['email'];
                        $users->phone = $_POST['phone'];
                        $users->accesses = 2;
                        if ($role==0)
                        {
                        $users->confirm = 1;
                        }
                        if($role==1)
                        {
                            $documents = ORM::factory('document');
                            $prefix = "document";
                            $new_name = $prefix . uniqid().".jpg";
                            $documents->document1 = $new_name;
                            $validation = Validation::factory($_FILES)
                                ->rules( TRUE , array(
                                    array('Upload::image'),
                                ));
                            if ($validation->check())
                            {
                                Upload::save($validation['document1'], $new_name, $this->uploads_dir());                 
                                Session::instance()->set('message', 'Image is successfully uploaded');
                            }
                            $documents->save();
                            $users->document_id = $documents->id;
                            $users->save();
                        }
                        $users->save();
                    }
                }
            /////////////////////////////////////////////////////////////////////////
            // це для пошти
//            $config = Kohana::$config->load('email');
//            Email::connect($config);
//            $to = 'bilousvitek@gmail.com';
//            $subject = 'Registration';
//            $from = $_POST['email'];
//            $message = 'Ваш логін:'.$_POST['login'].' Ваш пароль:'.$_POST['login'];
//            Email::send($to, $from, $subject, $message, $html = false);  
            /////////////////////////////////////////////////////////////////////////       
        }
        else
        {
            echo "<script>alert('Введіть ще раз значення з картинки!')</script>";
        }
        }
    }
    public function action_addUs()
    {
        $captcha = Captcha::instance('default'); 
        $content = View::factory('pages/addUs')
            ->bind('captcha', $captcha);
        $this->addUser(0);     
        $this->template->content = $content;
    }       
     public function action_addRealtor()
     {
         $captcha = Captcha::instance('default'); 
         $content = View::factory('pages/addRealtor')
             ->bind('captcha', $captcha);
         $this->addUser(1);
         $this->template->content = $content;
     }
     public function action_addCompany()
     {   $d=1;
         $captcha = Captcha::instance('default');
         $content = View::factory('pages/addCompany')
             ->bind('captcha', $captcha);
         $this->addUser(1);
         $this->template->content = $content;
     }
        
    public function deleteUser()
    {
        if(isset($_POST['delete']))
        {
        ORM::factory('user', $_POST['id'])
        ->delete();
        }
    }
}